package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("dish")
public class DishController {

    /**
     * 新增菜品
     */
    @Autowired
    public DishService dishService;

    @PostMapping
    @CacheEvict(cacheNames = "diah",allEntries = true)//删除所有以dish开头的缓存
    public R<String> save(@RequestBody DishDto dishDto) {

        dishService.saveDishWithFlavor(dishDto);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     */
    @GetMapping("page")
    public R page(Integer page, Integer pageSize, String name) {
        Page<DishDto> pageInfo = dishService.pageDishKDto(page, pageSize, name);
        return R.success(pageInfo);
    }

    /**
     * 根据id回显数据
     */
    @GetMapping("{id}")
    public R getById(@PathVariable Long id) {
        DishDto dishDto = dishService.getDishByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 根据ID修改数据
     */
    @PutMapping
    @CacheEvict(cacheNames = "diah",allEntries = true)//删除所有以dish开头的缓存
    public R<String> updelete(@RequestBody DishDto dishDto) {

        dishService.putById(dishDto);
        return R.success("修改成功");
    }


    /**
     * 批量删除
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "diah",allEntries = true)//删除所有以dish开头的缓存
    public R<String> deleteByIds(Long[] ids) {

        dishService.deleteByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 停售，启售
     */
    @PostMapping("status/{status}")
    @CacheEvict(cacheNames = "diah",allEntries = true)//删除所有以dish开头的缓存
    public R statys(@PathVariable Integer status
            , Long[] ids) {
        //update dish set status=? where id in(?);

        dishService.lambdaUpdate()
                .set(status != null, Dish::getStatus, status)
                .in(Dish::getId, ids)
                .update();
        return R.success("修改成功");
    }

//    /**
//     * 根据id查菜品
//     */
//    @GetMapping("list")
//    public R<List<Dish>> list(Long categoryId, String name) {
//        List<Dish> list = dishService.lambdaQuery()
//                .eq(categoryId != null, Dish::getCategoryId, categoryId)
//                .like(StringUtils.isNotBlank(name), Dish::getName, name)
//                .eq(Dish::getStatus, 1)
//                .list();
//        return R.success(list);
//    }

//    @Autowired
//    private CacheManager cacheManager;

    /**
     * 根据id查菜品
     */
    @GetMapping("list")
    @Cacheable(cacheNames = "diah",key = "#categoryId+'_'+#name",unless = "#result.data.size()==0")
    public R<List<DishDto>> listDishDto(Long categoryId, String name) {

        //如果为空就下来了，从数据库中查询数据
        List<DishDto>  dishDtoList=  dishService.listDishDto(categoryId,name);
        return R.success(dishDtoList);
    }
}
